/******************************************************************************
 * Copyright (C) 2008 Adam Parker. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 ******************************************************************************/
 
import java.io.*;
import javax.swing.*;
import javax.swing.table.*;

// A class that generates an XML file of the table for saving of all information
public class XML
{
	private JTable table = null;
	private JTable names = null;
	private CellData[][] info = null;
	private StudentData[] student = null;
	private AssignmentData[] assignment = null;
	private TableColumnModel model = null;
	private File xmlFile = null;
	private int numCols = 0;
	private int numRows = 0;

	public XML(JTable pTable, JTable pNames, CellData[][] pInfo, StudentData[] pStudent, AssignmentData[] pAssignment, TableColumnModel pModel)
	{
		table = pTable;
		names = pNames;
		info = pInfo;
		student = pStudent;
		assignment = pAssignment;
		model = pModel;
		numCols = pTable.getColumnCount();
		numRows = pTable.getRowCount();
		//xmlFile = new File(System.getProperty("user.dir") + "/info.xml");
		//System.out.println(columnModel.getColumn(index).getHeaderValue()
	}
	
	public void createXML(String pAbsPath) throws IOException
	{
		xmlFile = new File(pAbsPath);
		BufferedWriter output = new BufferedWriter(new FileWriter(xmlFile));
		output.write("<root>\n");
		for(int i = 0; i < numCols; i++)
		{
			output.write("\t<assignment name=\"" + model.getColumn(i).getHeaderValue() + "\">\n");
			output.write("\t\t<description>" + assignment[i].getAssignment() + "</description>\n");
			output.write("\t\t<points>" + assignment[i].getPoints() + "</points>\n");
			output.write("\t\t<duedate>" + assignment[i].getDueDate() + "</duedate>\n");
			output.write("\t\t<category>" + assignment[i].getCategory() + "</category>\n");
			output.write("\t</assignment>\n");
			
		}
		for(int i = 0; i < numRows; i++)
		{
			output.write("\t<row name=\"" + names.getValueAt(i, 0) + "\">\n");
			output.write("\t<number>" + student[i].getNumber() + "</number>\n");
			output.write("\t<email>" + student[i].getEmail() + "</email>\n");
			for(int j = 0; j < numCols; j++)
			{
				output.write("\t\t<column>\n");				
				output.write("\t\t\t<grade>" + table.getValueAt(i, j) + "</grade>\n");
				output.write("\t\t\t<late>" + info[i][j].getLate() + "</late>\n");
				output.write("\t\t\t<complete>" + info[i][j].getComplete() + "</complete>\n");
				output.write("\t\t\t<info>" + info[i][j].getInfo() + "</info>\n");
				output.write("\t\t</column>\n");
			}
			output.write("\t</row>\n");
		}
		output.write("</root>\n");
		output.close();
	}
	
	public void loadXML(String pAbsPath) throws IOException
	{
	
		/*	table = pTable;
		names = pNames;
		info = pInfo;
		student = pStudent;
		assignment = pAssignment;
		model = pModel;*/
		for(int i = 0; i < numRows; i++)
		{
			for(int j = 0; j < numCols; j++)
			{
				info[i][j] = new CellData();
				table.setValueAt("", i, j);
			}
		}
				
		for(int i = 0; i < numRows; i++)
		{
			student[i] = new StudentData();
			table.setValueAt("", i, 0);
		}
			
		for(int i = 0; i < numCols; i++)
		{
			assignment[i] = new AssignmentData();
			model.getColumn(i).setHeaderValue(" ");
		}
			
		String temp = "";
		xmlFile = new File(pAbsPath);
		BufferedReader input = new BufferedReader(new FileReader(xmlFile));
		if(input.readLine().equals("<root>"))
		{
			for(int i = 0; i < numCols; i++)
			{
				model.getColumn(i).setHeaderValue(getNameAttribute(input.readLine()));
				assignment[i].setAssignment(getContent(input.readLine()));
				assignment[i].setPoints(getContent(input.readLine()));
				assignment[i].setDueDate(getContent(input.readLine()));
				assignment[i].setCategory(getContent(input.readLine()));
				input.readLine();
			}
			for(int i = 0; i < numRows; i++)
			{
				temp = input.readLine();
				if(!getNameAttribute(temp).equals("null"))
					names.setValueAt(getNameAttribute(temp), i, 0);
				student[i].setNumber(getContent(input.readLine()));
				student[i].setEmail(getContent(input.readLine()));
				for(int j = 0; j < numCols; j++)
				{
					input.readLine();
					temp = input.readLine();
					if(!getContent(temp).equals("null"))
						table.setValueAt(getContent(temp), i, j);
					info[i][j].setLate(Integer.parseInt(getContent(input.readLine())));
					if(getContent(input.readLine()).equals("true"))
						info[i][j].setComplete(true);
					else
						info[i][j].setComplete(false);
					info[i][j].setInfo(getContent(input.readLine()));
					input.readLine();
				}
				input.readLine();
			}
		}
		input.close();
	}
	
	public String getContent(String pInput)
	{
		int begin = pInput.indexOf(">");
		int end = pInput.indexOf("<", begin);
		//System.out.println(pInput.substring(begin + 1, end));
		return pInput.substring(begin + 1, end);
	}
	
	public String getNameAttribute(String pInput)
	{
		int begin = pInput.indexOf("\"");
		int end = pInput.indexOf("\"", begin + 1);
		//System.out.println(" " + begin + " " + end);
		return pInput.substring(begin + 1, end);
	}
}